package com.bt.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bt.annotation.RequiresPermissionsDesc;
import com.bt.pojo.DtsTopic;
import com.bt.service.DtsTopicService;
import com.bt.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/26 14:15
 **/
@RequestMapping("/admin/topic")
@CrossOrigin
@RestController
public class AdminTopicController {
    @Autowired
    private DtsTopicService dtsTopicServiceImpl;
    /**
     * http://localhost:8080/admin/topic/list?page=1&limit=20&title=12&subtitle=3&sort=add_time&order=desc
     * get
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:topic:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "查询专题")
    public Object list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, String title, String subtitle, String sort, String order){
        if (page<1){
            page=1;
        }
        if (limit<1){
            limit=20;
        }
        IPage<DtsTopic> IPage= dtsTopicServiceImpl.findDtsTopicByPage(page,limit,title,subtitle,sort,order);
        Map data=new HashMap();
        data.put("total",IPage.getTotal());
        data.put("items",IPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/topic/create
     * post
     */
    @PostMapping("/create")
    @RequiresPermissions("admin:topic:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "添加专题")
    public Object create(@RequestBody DtsTopic dtsTopic){
        if (StringUtils.isEmpty(dtsTopic.getTitle())){
            return ResponseUtil.badArgument();
        }
        if (StringUtils.isEmpty(dtsTopic.getSubtitle())){
            return ResponseUtil.badArgument();
        }
        try {
            dtsTopicServiceImpl.insertDtsTopic(dtsTopic);
            return ResponseUtil.ok(dtsTopic);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }

    }
    /**
     * http://localhost:8080/admin/topic/update
     * post
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:topic:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "编辑专题")
    public Object update(@RequestBody DtsTopic dtsTopic){
        if (dtsTopic.getId()==null){
            return ResponseUtil.badArgument();
        }
        if (StringUtils.isEmpty(dtsTopic.getTitle())){
            return ResponseUtil.badArgument();
        }
        if (StringUtils.isEmpty(dtsTopic.getSubtitle())){
            return ResponseUtil.badArgument();
        }
        try {
            dtsTopicServiceImpl.updateDtsTopic(dtsTopic);
            return ResponseUtil.ok(dtsTopic);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }

    }
    /**
     * http://localhost:8080/admin/topic/delete
     * post
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "推广管理"}, button = "删除专题")
    public Object delete(@RequestBody DtsTopic dtsTopic){
        if (dtsTopic.getId()==null){
            return ResponseUtil.badArgument();
        }
        try {
            dtsTopicServiceImpl.deleteDtsTopic(dtsTopic);
            return ResponseUtil.ok(dtsTopic);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }

    }
}
